package com.oauth.application.client.components;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.oauth.application.client.rpc.ApplicationAddRemoteServiceAsync;
import com.oauth.components.client.beans.ApplicationClientBean;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;

public class AddButton extends Button {

	public AddButton(final ApplicationClientBean pApplicationClientBean,
			final ApplicationAddRemoteServiceAsync pApplicationAddService) {

		super("Add");
		final Long applicationId = pApplicationClientBean.getId();
		addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent pEvent) {
				pApplicationAddService.subscribeToApplication(applicationId,new OAuthAsnyCallBack<String>() {
					public void onSuccess(String result) {
					    if(result.equals("success")){
					    	Window.Location.assign(pApplicationClientBean.getAddUrl());
					    }else{
					    	Window.alert("Unable to add application.Try again after some time");
					    }
					    
					}
				});
				
			}
			
		});
	}
	
	
}
